package com.community.demo.controller;

import com.community.demo.domain.User;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class PathController {

    @GetMapping("/")
    public String path(Model model){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal == null || "anonymousUser".equals(principal)) {
//            model.addAttribute("name", "anonymousUser");
            return "/error";
        }else {
            User user = (User) principal;
            model.addAttribute("user", user);
        }
        return "/blog";
    }
}
